package com.buanalintas.sandals.pos.receiptdetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.buanalintas.sandals.pos.entities.PosMasterSize;
import com.buanalintas.sandals.pos.entities.PosTransactionPurchaseOrderDetail;
import com.buanalintas.sandals.pos.entities.PosTransactionReceiptDetail;
import java.util.List;
import javax.persistence.EntityManager;

public class ReceiptDetail extends Page {

    private Field receiptDetailId;
    private Field receiptDetailReceiptId;
    private Field receiptDetailSizeId;
    private Field receiptDetailSize;
    private Field receiptDetailItemId;
    private Field receiptDetailQuantity;
    private Field receiptDetailPrice;
    private Field receiptDetailDescription;

    public ReceiptDetail() {
        super();
        setTheClass(PosTransactionReceiptDetail.class);

        receiptDetailId = new Field();
        receiptDetailId.setName("receiptDetailId");
        receiptDetailId.setCaption("id");
        receiptDetailId.setListProperties("");
        receiptDetailId.setInputProperties("");
        getFields().add(receiptDetailId);

        receiptDetailReceiptId = new Field();
        receiptDetailReceiptId.setName("receiptDetailReceiptId");
        receiptDetailReceiptId.setCaption("id");
        receiptDetailReceiptId.setListProperties("");
        receiptDetailReceiptId.setInputProperties("");
        getFields().add(receiptDetailReceiptId);

        receiptDetailItemId = new Field();
        receiptDetailItemId.setName("receiptDetailItemId");
        receiptDetailItemId.setCaption("item");
        receiptDetailItemId.setListProperties(" align=\"left\" width=\"20%\" nowrap");
        receiptDetailItemId.setInputProperties(" size=\"25\"");
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemCode", " Code", "width=\"5%\" nowrap"});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemName", " Item", "width=\"90%\""});
        receiptDetailItemId.setSelectorOrder("t.itemName");
        receiptDetailItemId.setRequired(true);
        getFields().add(receiptDetailItemId);
        
        receiptDetailSize = new Field();
        receiptDetailSize.setName("receiptDetailSize");
        receiptDetailSize.setCaption("size");
        receiptDetailSize.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        receiptDetailSize.setEntityRelation(PosMasterSize.class);
        receiptDetailSize.setInputType(Field.INPUT_CHECK_BOX);
        receiptDetailSize.setRequired(true);
        getFields().add(receiptDetailSize);

        receiptDetailSizeId = new Field();
        receiptDetailSizeId.setName("receiptDetailSizeId");
        receiptDetailSizeId.setCaption("size");
        receiptDetailSizeId.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        receiptDetailSizeId.setEntityRelation(PosMasterSize.class);
        receiptDetailSizeId.setRequired(true);
        getFields().add(receiptDetailSizeId);

        receiptDetailQuantity = new Field();
        receiptDetailQuantity.setName("receiptDetailQuantity");
        receiptDetailQuantity.setCaption("quantity");
        receiptDetailQuantity.setListProperties(" align=\"right\" width=\"5%\" nowrap");
        receiptDetailQuantity.setInputProperties(" size=\"5\"");
        receiptDetailQuantity.setRequired(true);
        getFields().add(receiptDetailQuantity);

        receiptDetailPrice = new Field();
        receiptDetailPrice.setName("receiptDetailPrice");
        receiptDetailPrice.setCaption("price");
        receiptDetailPrice.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        receiptDetailPrice.setInputProperties(" size=\"8\"");
        receiptDetailPrice.setRequired(true);
        receiptDetailPrice.setFormater(MoneyFormatter.getInstance());
        getFields().add(receiptDetailPrice);

        receiptDetailDescription = new Field();
        receiptDetailDescription.setName("receiptDetailDescription");
        receiptDetailDescription.setCaption("description");
        receiptDetailDescription.setListProperties(" width=\"70%\"");
        receiptDetailDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        receiptDetailDescription.setInputType(Field.INPUT_TEXT_AREA);
        getFields().add(receiptDetailDescription);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(receiptDetailItemId);
        getFieldsAdd().add(receiptDetailSize);
        getFieldsAdd().add(receiptDetailQuantity);
        getFieldsAdd().add(receiptDetailPrice);
        getFieldsAdd().add(receiptDetailDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(receiptDetailItemId);
        getFieldsEdit().add(receiptDetailSizeId);
        getFieldsEdit().add(receiptDetailQuantity);
        getFieldsEdit().add(receiptDetailPrice);
        getFieldsEdit().add(receiptDetailDescription);

        getFieldsList().clear();
        getFieldsList().add(receiptDetailItemId);
        getFieldsList().add(receiptDetailSizeId);
        getFieldsList().add(receiptDetailQuantity);
        getFieldsList().add(receiptDetailPrice);
        getFieldsList().add(receiptDetailDescription);

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);
    }

    @Override
    public void loadDetail(List datas, String id) {
        try {
            EntityManager em = getEntityManager();
            List<PosTransactionPurchaseOrderDetail> request = em.createQuery("SELECT t FROM PosTransactionPurchaseOrderDetail t WHERE t.purchaseOrderDetailPurchaseOrderId.purchaseOrderId='" + id + "'").setHint(Persistence.REFRESH_HINT, true).getResultList();
            datas.clear();
            for (PosTransactionPurchaseOrderDetail pd : request) {
                PosTransactionReceiptDetail rd = new PosTransactionReceiptDetail();
                rd.setReceiptDetailItemId(pd.getPurchaseOrderDetailItemId());
                rd.setReceiptDetailSizeId(pd.getPurchaseOrderDetailSizeId());
                rd.setReceiptDetailQuantity(pd.getPurchaseOrderDetailUnsent());
                rd.setReceiptDetailPrice(pd.getPurchaseOrderDetailPrice());
                rd.setReceiptDetailDescription(pd.getPurchaseOrderDetailDescription());
                datas.add(rd);
            }
        } catch (Exception e) {
            Logger.getInstance().log(e);
        }
    }
}
